/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.Articolo;
import ejb.ArticoloFacade;
import ejb.BusinessLogic;
import ejb.Carrello;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Lella
 */
public class CreaCarrello extends HttpServlet {

    @EJB
    private BusinessLogic businessLogic;
    @EJB
    private ArticoloFacade articoloFacade;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            HttpSession s = request.getSession();
            String username = (String) s.getAttribute("username");
            List<Carrello> cart = businessLogic.getUserCarrello(username);
            Carrello c;
            if(!cart.isEmpty()){
            out.println("<form name='carrello' method='POST' action='Controller' id='carrello'><fieldset>");
            for (int i = 0; i < cart.size(); i++) {
                c = cart.get(i);
                Articolo a = articoloFacade.find(Long.valueOf(c.getArticolo()));
                out.println("<div class='prodotto_carrello'>");
                out.println("<div class='elem_carrello_top'><div><img src='images/" + a.getFoto() + "' alt='' width='60' height='100' class='left' /></a><div class='marca'>" + a.getModello() + " </div>");
                out.println("<div class='modello'>" + a.getModello() + "</div>");
                out.println("<div class='prezzo' name='prezzo_"+ c.getId() + "'>" + a.getPrezzo_vendita() * c.getQuantita() + " €</div>");
                out.println("<div><input type='hidden' name='prezzo_singolo_" + c.getId() + "' value='"+ a.getPrezzo_vendita() + "'/></div></div></div>");
                out.println("<div class='elem_carrello_bottom'><input type='checkbox' id='compra" + c.getId() + "' name='" + c.getId() + "' value='compra' onchange=chkCheckbox(compra"+ c.getId() + ",elimina"+ c.getId() + ")> Compra</input><br>");
                out.println("<input type='checkbox' id='elimina" + c.getId() + "' name='" + c.getId() + "' value='elimina' onchange=chkCheckbox(elimina"+ c.getId() +",compra"+ c.getId() + ")> Elimina</input>");
                out.println("<input type='hidden' name='" + a.getModello() + "' value='" + a.getModello() + "'/>");
                out.println("<div class='carrello_right'>Quantit&agrave;<select name='quantita_"+ c.getId() +"' onchange=setPrezzo("+ c.getId() +") >");
                int j = 0;
                while(j < c.getQuantita())
                       out.println("<option"+ (++j == c.getQuantita()? " selected='selected'>" : ">") + j + "</option>");
                out.println("<select></div></div></div>");
            }
            out.println("</fieldset><center><input type='submit' name='carrello' value='Conferma'/></center></form>");
            }
            else
                out.println("<center><h3>Non sono presenti articoli nel carrello</h3></center>");
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
